package com.gitee.aurora.server.controller;


import com.gitee.aurora.core.utils.ResultVO;
import com.gitee.aurora.core.utils.ResultVOUtils;
import com.gitee.aurora.server.service.RuleConfigService;
import com.gitee.aurora.server.vo.RuleVo;
import com.gitee.aurora.utils.PageParam;
import com.gitee.aurora.utils.Pagination;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * 核验参数配置功能
 * @author gaoxu
 */
@Controller
@ResponseBody
@RequestMapping("RuleConfig")
public class RuleConfigController {
    @Autowired
    private RuleConfigService ruleConfigService;

    /**
     * 参数查询
     */
    @RequestMapping("queryRule")
    @ApiOperation(value = "参数查询", notes = "参数查询")
    public ResultVO<Pagination> queryRule(@RequestBody RuleVo ruleVo, PageParam pageParam){
        Pagination pagination = ruleConfigService.queryRule(ruleVo, pageParam);
        return ResultVOUtils.success(pagination);
    }

    /**
     * 参数更新
     */
    @RequestMapping("updataRule")
    @ApiOperation(value = "参数更新", notes = "参数更新")
    public ResultVO updataRule(@RequestBody RuleVo ruleVo){
        return ruleConfigService.updataRule(ruleVo);
    }

    /**
     * 参数新增
     */
    @RequestMapping("addRule")
    @ApiOperation(value = "参数新增", notes = "参数新增")
    public ResultVO addRule(@RequestBody RuleVo ruleVo){
        if (ruleVo == null || ruleVo.getTableName() == null || "".equals(ruleVo.getTableName()) || ruleVo.getDeptCode() == null || "".equals(ruleVo.getDeptCode())){
            return ResultVOUtils.error("提交参数缺失");
        }
        return ruleConfigService.addRule(ruleVo);
    }

    /**
     * 参数删除
     */
    @RequestMapping("deleteRule")
    @ApiOperation(value = "参数删除", notes = "参数删除")
    public ResultVO deleteRule(@RequestBody List<RuleVo> ruleVoList){
        return ruleConfigService.deleteRule(ruleVoList);
    }

    /**
     * 查询表字段接口
     */
    @PostMapping("/getDBTableField")
    @ApiOperation(value = "查询表字段接口", notes = "查询表字段接口")
    public ResultVO getDBTableField(@RequestBody RuleVo ruleVo){
        return ruleConfigService.getDBTableField(ruleVo);
    }

    /**
     * 查询核查表列表
     */
    @PostMapping("/getCheckTableList")
    @ApiOperation(value = "查询核查表列表", notes = "查询核查表列表")
    public ResultVO<List<String>> getCheckTableList(){
        List<String> tableList = new ArrayList<>();
        try {
            tableList = ruleConfigService.getCheckTableList();
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultVOUtils.success(tableList);
    }

}
